package com.jrx.anytxn.investor.service.impl;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.investor.dto.loan.AmRepaymentPlanInvestorInfoRes;
import com.jrx.anytxn.investor.entity.AmRepaymentPlanInvestorInfo;
import com.jrx.anytxn.investor.mapper.ext.ExtAmRepaymentPlanInvestorInfoMapper;
import com.jrx.anytxn.investor.service.IRepaymentPlanInvestorInfoService;
import com.jrx.anytxn.param.dto.investor.PrInvestorTableRes;
import com.jrx.anytxn.param.service.investor.IPrInvestorTableService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 还款顺序实现类
 *
 * @author 黄秋平
 * @create 2020/6/18
 */
@Service
public class RepaymentPlanInvestorInfoServiceImpl implements IRepaymentPlanInvestorInfoService {

    private static final Logger logger = LoggerFactory.getLogger(RepaymentPlanInvestorInfoServiceImpl.class);

    @Resource
    private ExtAmRepaymentPlanInvestorInfoMapper extAmRepaymentPlanInvestorInfoMapper;

    @Resource
    private IPrInvestorTableService prInvestorTableService;

    @Override
    public List<AmRepaymentPlanInvestorInfoRes> getRepaymentListByLoanIdAndCusIdAndInveId(String tenantId, String customerId, String loanId, String investorId) throws TxnBizException {
        List<AmRepaymentPlanInvestorInfo> repaymentPlanInfoList=extAmRepaymentPlanInvestorInfoMapper.selectPlanListByCusIdAndLoanIdAndInveId(customerId,tenantId,loanId,investorId);
        return BeanMapping.copyList(repaymentPlanInfoList, AmRepaymentPlanInvestorInfoRes.class);
    }

    @Override
    public AmRepaymentPlanInvestorInfoRes getRepaymentInfoByInvesLoanIdAndCusIdAndTenor(String tenantId, String customerId, String investorLoanId, Integer billingTenor) throws TxnException {
        AmRepaymentPlanInvestorInfoRes res;
        AmRepaymentPlanInvestorInfo repaymentPlanInvestorInfo=extAmRepaymentPlanInvestorInfoMapper.selectByInvestorLoanIdAndTenor(customerId,tenantId,investorLoanId,billingTenor);
        if(null==repaymentPlanInvestorInfo){
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getCode());
        }
        res=BeanMapping.copy(repaymentPlanInvestorInfo,AmRepaymentPlanInvestorInfoRes.class);
        //获取出资方信息
        PrInvestorTableRes prInvestorTableRes=prInvestorTableService.findByTenIdAndInvestorId(tenantId,repaymentPlanInvestorInfo.getInvestorId());
        res.setChineseName(prInvestorTableRes.getChineseName());
        return res;
    }
}
